package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.SocietyDao;
import com.gzy.studentsociety.dao.SocietyDaoImpl;
import com.gzy.studentsociety.dao.StudentDao;
import com.gzy.studentsociety.dao.StudentDaoImpl;
import com.gzy.studentsociety.entity.Society;
import com.gzy.studentsociety.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version: java version 1.8
 * @Author: Chuan Liu
 * @description:
 * @date: 2024-10-31 10:38
 */
@WebServlet(name = "SocietyServlet", value = "/society/*")
public class SocietyServlet extends RouteServlet {
    private SocietyDao societyDao = new SocietyDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();

    protected Class getMyClass() {
        return this.getClass();
    }

    public String mainIndex(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        List<Society> list = null;
        try {
            list = societyDao.getAllSociety();
        } catch (SQLException e) {
            e.printStackTrace();
            handleException(e, req, res);
        }
        list = list.stream().map(i -> {
            try{
                i.setCreator(studentDao.findById(i.getSocietyCreator()));
            }catch (SQLException e){
                req.setAttribute("exception",e);
            }
            return i;
        }).collect(Collectors.toList());
        req.setAttribute("societies",list);
        // req.getRequestDispatcher("/WEB-INF/jsp/mainIndex.jsp").forward(req,res);
        return "mainIndex";
    }

    public String addSociety(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        Society society = new Society();
        society.setSocietyIntro(intro);
        society.setSocietyName(name);
        society.setSocietyStatus(1);
        // 获取session 中登录者为创建者
        String number = (String)(req.getSession().getAttribute("student"));
        try {
            society.setSocietyCreator(studentDao.findByNumber(number).getStudentId());
        } catch (SQLException e) {
            e.printStackTrace();
            handleException(e, req, res);
        }
        society.setSocietyCreateDate(new Date());
        int result = -1;
        try {
            result = societyDao.insertSociety(society);
        } catch (SQLException e) {
            e.printStackTrace();
            handleException(e, req, res);
        }
        res.sendRedirect(req.getContextPath() + "/society/mainIndex");
        return "direct";
    }

    public Object updateSociety(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException{
        String idStr = req.getParameter("id");
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        Integer id = idStr != null ? Integer.valueOf(idStr):null;
        Society society = new Society();
        society.setSocietyId(id);
        society.setSocietyName(name);
        society.setSocietyIntro(intro);

        Map<String,Object> map = new HashMap<String,Object>();
        Integer result = null;
        if(id == null){
            map.put("message","id错误");
        }else{
            try {
                result = societyDao.updateSociety(society);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                map.put("message",throwables);
            }
            map.put("message",result);
        }
        return map;

    }
    public Object changeStatus(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException{
        String idStr = req.getParameter("id");
        String statusStr= req.getParameter("status");
        Integer id = Integer.valueOf(idStr);
        Integer status = Integer.parseInt(statusStr);
        Society society = new Society();
        society.setSocietyId(id);
        society.setSocietyStatus(status);

        int result = -1;
        try {
            result = societyDao.updateSociety(society);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("exception",throwables);
            return "error";
        }
        if(result == 1){
            return result;
        }else{
            HashMap<String,String> map = new HashMap<String,String>();
            map.put("message","错误修改");
            return map;
        }
    }
    public Object addPage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String number = (String)session.getAttribute("student");
        Society society = null;
        try {
            society = societyDao.queryByStudentNumber(number);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables,req,res);
        }
        req.setAttribute("society",society);
        return "member/addStudent";
    }

}
